package cn.youframe.comp.tree;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: Administrator
 * DateTime: 13-3-13 下午8:43
 * Desc:
 */
public class TreeHelper {
    public TreeHelper() {
    }

    /**
     * buildTree
     *
     * @param treeName String
     * @return Tree
     */
    public static Tree buildTree(String treeName) {
        Tree tree=new Tree();
        tree.setTreeName(treeName);
        return tree;
    }
    /**
     * buildTree 根据节点列表创建树对象，列表必须符合树的规则
     *
     * @param nlist List
     * @return Tree
     */
    public static <T extends INode> Tree<T> buildTree(List<T> nlist) {
        return buildTree(null,nlist);
    }
    /**
     * buildTree 根据根节点ID和节点列表创建树对象，列表必须符合树的规则
     *
     * @param rootID String
     * @param nlist List
     * @return Tree
     */
    public static <T extends INode> Tree<T> buildTree(String rootID,List<T> nlist) {
        try {
            if((nlist==null)||(nlist.size()==0)){
                return null;
            }
            TreeNode<T> node = null;
            Map<String,TreeNode> nodesMap = new HashMap<String,TreeNode>();
            for(T inode:nlist){
                node=new TreeNode<T>();
                node.setNodeID(inode.getNodeID());
                node.setParentID(inode.getParentID());
                node.setNodeName(inode.getNodeName());
                node.setNodeObj(inode);
                nodesMap.put(node.getNodeID(),node);
            }
            TreeNode<T> pNode = null;
            Tree<T> tree = new Tree<T>();
            for(T inode2:nlist){
                if(rootID==null){
                    if((inode2.getParentID()==null)||(inode2.getParentID().equalsIgnoreCase(""))){
                        node=nodesMap.get(inode2.getNodeID());
                        tree.setTreeName(inode2.getNodeName());
                        tree.setRoot(node);
                    }else{
                        node=nodesMap.get(inode2.getNodeID());
                        pNode=nodesMap.get(inode2.getParentID());
                        if(pNode!=null){
                            pNode.addChild(node);
                            node.setParent(pNode);
                        }
                    }
                }else{
                    if(inode2.getNodeID().equalsIgnoreCase(rootID)){
                        node=nodesMap.get(inode2.getNodeID());
                        tree.setTreeName(inode2.getNodeName());
                        tree.setRoot(node);
                    }else{
                        node=nodesMap.get(inode2.getNodeID());
                        pNode=nodesMap.get(inode2.getParentID());
                        if(pNode!=null){
                            pNode.addChild(node);
                            node.setParent(pNode);
                        }
                    }
                }
            }
            return tree;
        } catch (Exception ex) {
            return null;
        }
    }
}
